package com.sanxiang.service.impl;

import com.sanxiang.service.FileService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
@Service
public class FileServiceImpl implements FileService {
    @Override
    public String Save_File(MultipartFile file) {
        String fname = "";
        String fail_msg = "上传失败";
        // 如果目录不存在则创建
        File uploadDir = new File("upload");
        if (!uploadDir.exists()) {
            uploadDir.mkdir();
        }
        fname = file.getOriginalFilename();//获取原文件名

        File localFile = new File(uploadDir.getAbsolutePath() + "\\" + fname);
        try {
            file.transferTo(localFile); //把上传的文件保存至本地

        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(fail_msg);
        }
        return fname;
    }

}
